<template>
  <div class="login__bg">
    <div class="login__main">
      <h1>WIFI访问认证系统</h1>
      <el-form ref="loginForm" class="base-form" :model="loginForm" :rules="loginRules" label-width="80px">
        <el-form-item label="手机号" prop="phone">
          <el-input v-model="loginForm.phone" placeholder="请输入手机号" type="text" prefix-icon="el-icon-phone">
          </el-input>
        </el-form-item>
        <el-form-item label="姓名" prop="name">
          <el-input v-model="loginForm.name" placeholder="请输入姓名" type="text"
                    @keyup.enter.native="handleLogin">
          </el-input>
        </el-form-item>
        <el-button class="form-btn" :loading="loading"
                   style="width: 100%; background: linear-gradient(-5deg, #6394ff, #2878ff); height: 39px"
                   type="primary" @click.native.prevent="handleLogin">
          <span v-if="!loading">登 记</span>
          <span v-else>登 记 中...</span>
        </el-button>
      </el-form>
    </div>
    <div class="footer-box">
      <span>Copyright © 2022-2023 安信 版权所有</span>
    </div>
  </div>
</template>

<script>
export default {
  name: "MyLogin",
  data() {
    return {
      loginForm: {
        phone: undefined,
        name: undefined,
      },
      loginRules: {
        phone: [{required: true, trigger: 'blur', message: '请输入您的手机号'}],
        name: [{required: true, trigger: 'blur', message: '请输入您的姓名'}],
      },
      loading: false,
    }
  },
  methods: {
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true;
          this.$store.dispatch('LoginByUsername', this.loginForm).then(() => {
            this.loading = false;
            this.$router.push({path: '/'});
          }).catch(() => {
            this.loading = false;
          })
        } else {
          console.log('error submit!!');
          return false;
        }
      });

    }
  }
}
</script>

<style scoped lang="scss">
.login__bg {
  width: 100%;
  height: 100%;
  display: flex;
  background-repeat: no-repeat;
  background-size: 100% 100%;
  align-items: center;
  flex-direction: column;
  justify-content: space-evenly;
  background-image: url("@/assets/images/bg.png");
}

.login__bg .login__main {
  width: 400px;
  height: 300px;
  background: #ffffff;
  box-shadow: 0 4px 35px 0 rgba(3, 3, 3, 0.07);
  border-radius: 8px;
  padding: 30px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: space-between;
  .base-form{
    height: 200px;
    width: 100%;
    .form-btn{
      margin-top: 30px;
    }
  }
}
.footer-box {
  width: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  color: #fff;
  font-size: 12px;
}

.footer-box .footer-main {
  display: flex;
  align-items: center;
  justify-content: center;
  margin-top: 10px;
}
</style>